package com.jinniulive.platform.module.video;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jinniulive.platform.MyApp;
import com.jinniulive.platform.R;
import com.jinniulive.platform.adapter.VideoFragmentPagerAdapter;
import com.jinniulive.platform.bean.LoginInfo;
import com.jinniulive.platform.bean.VideoListInfo;
import com.jinniulive.platform.urls.NetConfig;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class VideoFragment extends Fragment {


    private TabLayout tabLayout;

    private ViewPager viewPager;
    private VideoFragmentPagerAdapter mAdapter;
    private List<ChildVideoFragment> mData;
    private List<VideoListInfo.VideoDataBean.VtlstBean> vtlst;

    public VideoFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_video, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MyApp myApp = (MyApp) getActivity().getApplication();
        LoginInfo loginInfo = myApp.getLoginInfo();
        String tokenID = loginInfo.getUserslist().get(0).getTokenID();
        int userID = loginInfo.getUserslist().get(0).getUserID();
        initView(view);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        String path = String.format(NetConfig.VIDEOLIST, tokenID, String.valueOf(userID), 1, 10, 1);
        RequestParams params = new RequestParams(path);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                VideoListInfo videoListInfo = gson.fromJson(result, VideoListInfo.class);
                if (videoListInfo.getErrorcode() == 200) {
                    List<VideoListInfo.VideoDataBean> videoData = videoListInfo.getVideoData();
                    vtlst = videoData.get(0).getVtlst();
                    mData = new ArrayList<>();
                    for (int i = 0; i < vtlst.size(); i++) {
                        ChildVideoFragment childVideoFragment = ChildVideoFragment.getChildVideoFragment(vtlst.get(i).getTypeid());
                        mData.add(childVideoFragment);
                    }
                    mAdapter = new VideoFragmentPagerAdapter(getChildFragmentManager(), mData, vtlst);
                    if(vtlst.size()<5){
                        tabLayout.setTabMode(TabLayout.MODE_FIXED);
                    }else {
                        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                    }
                    viewPager.setAdapter(mAdapter);
                    tabLayout.setupWithViewPager(viewPager);

                } else {
                    Toast.makeText(getContext(), "网络错误", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }


    private void initView(View view) {
        tabLayout = (TabLayout) view.findViewById(R.id.video_tablayout);
        viewPager = (ViewPager) view.findViewById(R.id.video_vp);
    }

}
